package com.netease.nim.demo.session.adapter;

import java.util.List;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.netease.nim.demo.common.adapter.TAdapter;
import com.netease.nim.demo.common.adapter.TAdapterDelegate;
import com.netease.nim.demo.session.model.MsgViewHolderType;
import com.netease.nim.demo.session.viewholder.MsgViewHolder;
import com.netease.nim.demo.session.viewholder.MsgViewHolder.ViewHolderEventListener;
import com.netease.nim.demo.session.viewholder.MsgViewHolderAVChatLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderAVChatRight;
import com.netease.nim.demo.session.viewholder.MsgViewHolderActivityLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderActivityRight;
import com.netease.nim.demo.session.viewholder.MsgViewHolderAudioLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderAudioRight;
import com.netease.nim.demo.session.viewholder.MsgViewHolderDefCustomLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderDefCustomRight;
import com.netease.nim.demo.session.viewholder.MsgViewHolderFileLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderFileRight;
import com.netease.nim.demo.session.viewholder.MsgViewHolderGuessLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderGuessRight;
import com.netease.nim.demo.session.viewholder.MsgViewHolderNotification;
import com.netease.nim.demo.session.viewholder.MsgViewHolderPictureLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderPictureRight;
import com.netease.nim.demo.session.viewholder.MsgViewHolderTextLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderTextRight;
import com.netease.nim.demo.session.viewholder.MsgViewHolderVideoLeft;
import com.netease.nim.demo.session.viewholder.MsgViewHolderVideoRight;

public class MsgAdapter extends TAdapter {

    private ViewHolderEventListener eventListener;

    public MsgAdapter(Context context, List<?> items, TAdapterDelegate delegate) {
        super(context, items, delegate);
    }

    private static SparseArray<Class> viewHolders = new SparseArray<Class>();

    static {
        viewHolders.put(MsgViewHolderType.NOTIFICATION, MsgViewHolderNotification.class);
        viewHolders.put(MsgViewHolderType.TEXT_RIGHT, MsgViewHolderTextRight.class);
        viewHolders.put(MsgViewHolderType.TEXT_LEFT, MsgViewHolderTextLeft.class);
        viewHolders.put(MsgViewHolderType.PICTURE_LEFT, MsgViewHolderPictureLeft.class);
        viewHolders.put(MsgViewHolderType.PICTURE_RIGHT, MsgViewHolderPictureRight.class);
        viewHolders.put(MsgViewHolderType.AUDIO_LEFT, MsgViewHolderAudioLeft.class);
        viewHolders.put(MsgViewHolderType.AUDIO_RIGHT, MsgViewHolderAudioRight.class);
        viewHolders.put(MsgViewHolderType.VIDEO_LEFT, MsgViewHolderVideoLeft.class);
        viewHolders.put(MsgViewHolderType.VIDEO_RIGHT, MsgViewHolderVideoRight.class);
        //TODO fw location
//        viewHolders.put(MsgViewHolderType.LOCATION_LEFT, MsgViewHolderLocationLeft.class);
//        viewHolders.put(MsgViewHolderType.LOCATION_RIGHT, MsgViewHolderLocationRight.class);
        viewHolders.put(MsgViewHolderType.GUESS_LEFT, MsgViewHolderGuessLeft.class);
        viewHolders.put(MsgViewHolderType.GUESS_RIGHT, MsgViewHolderGuessRight.class);
        viewHolders.put(MsgViewHolderType.CUSTOM_DEF_LEFT, MsgViewHolderDefCustomLeft.class);
        viewHolders.put(MsgViewHolderType.CUSTOM_DEF_RIGHT, MsgViewHolderDefCustomRight.class);
        viewHolders.put(MsgViewHolderType.FILE_LEFT, MsgViewHolderFileLeft.class);
        viewHolders.put(MsgViewHolderType.FILE_RIGHT, MsgViewHolderFileRight.class);
        viewHolders.put(MsgViewHolderType.CUSTOM_ACTIVITY_LEFT, MsgViewHolderActivityLeft.class);
        viewHolders.put(MsgViewHolderType.CUSTOM_ACTIVITY_RIGHT, MsgViewHolderActivityRight.class);
        viewHolders.put(MsgViewHolderType.AV_CHAT_LEFT, MsgViewHolderAVChatLeft.class);
        viewHolders.put(MsgViewHolderType.AV_CHAT_RIGHT, MsgViewHolderAVChatRight.class);
    }

    public Class getViewHolderByType(int viewHolderType) {
        return viewHolders.get(viewHolderType);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        Object viewHolder = view.getTag();
        if (viewHolder instanceof MsgViewHolder) {
            if (eventListener != null) {
                ((MsgViewHolder) viewHolder).setEventListener(eventListener);
            }
        }

        return view;
    }

    public int getViewTypeCount() {
        return viewHolders.size();
    }

    public void setEventListener(ViewHolderEventListener eventListener) {
        this.eventListener = eventListener;
    }
}
